﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using UDS.Components;

namespace UDS.SubModule.CM.Stat
{
    /// <summary>
    /// ContactedClient 的摘要说明。
    /// </summary>
    public partial class ContactedClientPage : Page
    {
        private DateTime begintime;
        private DateTime endtime;
        private string type;

        protected static string GetLinkMan(DataRow[] row)
        {
            if (row.Length != 0)
            {
                return (row[0]["Name"].ToString());
            }

            return ("");
        }

        protected static string GetCurStatus(string curstatus)
        {
            switch (curstatus.Split(',')[0])
            {
                case "trace":
                    return ("跟踪");
                case "boot":
                    return ("启动");
                case "commend":
                    return ("产品推荐");
                case "requirement":
                    return ("需求定义");
                case "submit":
                    return ("方案提交");
                case "negotiate":
                    return ("商务谈判");
                case "actualize":
                    return ("项目实施");
                case "traceservice":
                    return ("跟踪服务");
                case "last":
                    return ("收尾款");
            }
            return ("");
        }

        protected static string GetAddMan(DataRow[] row)
        {
            if (row.Length != 0)
            {
                return (row[0]["realname"].ToString());
            }
            return ("");
        }

        protected override void OnLoad(EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if (!Page.IsPostBack)
            {
                begintime = (Request.QueryString["begintime"] == null)
                                ? DateTime.Now
                                : DateTime.Parse(Request.QueryString["begintime"]);
                endtime = (Request.QueryString["endtime"] == null)
                              ? DateTime.Now
                              : DateTime.Parse(Request.QueryString["endtime"]);
                type = (Request.QueryString["type"] == null) ? "" : Request.QueryString["type"];

                ViewState["begintime"] = begintime;
                ViewState["endtime"] = endtime;
                ViewState["type"] = type;

                BindData(type);
            }
            else
            {
                begintime = DateTime.Parse(ViewState["begintime"].ToString());
                endtime = DateTime.Parse(ViewState["endtime"].ToString());
                type = ViewState["type"].ToString();
            }

            base.OnLoad(e);
        }

        private void BindData(string type)
        {
            var cm = new Components.CM();
            var ds = new DataSet();

            switch (type)
            {
                case "ac":
                    SqlDataReader dr_contactedclient = cm.GetContactedClientBySellmanID(0, begintime, endtime);
                    DataTable dt_contactedclient = Tools.ConvertDataReaderToDataTable(dr_contactedclient);
                    dt_contactedclient.TableName = "Client";
                    ds.Tables.Add(dt_contactedclient);
                    break;
                case "cc":
                    SqlDataReader dr_contactedcallinclient = cm.GetCallinClientBySellmanID(0, begintime, endtime);
                    DataTable dt_contactedcallinclient = Tools.ConvertDataReaderToDataTable(dr_contactedcallinclient);
                    dt_contactedcallinclient.TableName = "Client";
                    ds.Tables.Add(dt_contactedcallinclient);
                    break;
                case "nc":
                    SqlDataReader dr_newclient = cm.GetCallinClientBySellmanID(0, begintime, endtime);
                    DataTable dt_newclient = Tools.ConvertDataReaderToDataTable(dr_newclient);
                    dt_newclient.TableName = "Client";
                    ds.Tables.Add(dt_newclient);
                    break;
                case "neoc":
                    SqlDataReader dr_neoclient = cm.GetNegotiateClient();
                    DataTable dt_neoclient = Tools.ConvertDataReaderToDataTable(dr_neoclient);
                    dt_neoclient.TableName = "Client";
                    ds.Tables.Add(dt_neoclient);
                    break;
                case "neonc":
                    SqlDataReader dr_neonewclient = cm.GetNewNegotiateClient(begintime, endtime);
                    DataTable dt_neonewclient = Tools.ConvertDataReaderToDataTable(dr_neonewclient);
                    dt_neonewclient.TableName = "Client";
                    ds.Tables.Add(dt_neonewclient);
                    break;
                case "n3c":
                    SqlDataReader dr_new3client = cm.GetNew3StarClient(begintime, endtime);
                    DataTable dt_new3client = Tools.ConvertDataReaderToDataTable(dr_new3client);
                    dt_new3client.TableName = "Client";
                    ds.Tables.Add(dt_new3client);
                    break;
                case "3c":
                    SqlDataReader dr_3client = cm.Get3StarClient();
                    DataTable dt_3client = Tools.ConvertDataReaderToDataTable(dr_3client);
                    dt_3client.TableName = "Client";
                    ds.Tables.Add(dt_3client);
                    break;
            }

            var staff = new Components.Staff();
            SqlDataReader dr_staff = staff.GetAllStaffs();
            DataTable dt_staff = Tools.ConvertDataReaderToDataTable(dr_staff);
            dt_staff.TableName = "Staff";
            ds.Tables.Add(dt_staff);

            ds.Relations.Add("StaffID_Staff", ds.Tables["Client"].Columns["AddmanID"],
                             ds.Tables["Staff"].Columns["Staff_ID"], false);

            ltl_Client.Text = ds.Tables["Client"].Rows.Count.ToString();

            dgrd_Client.DataSource = ds.Tables["Client"].DefaultView;
            dgrd_Client.DataBind();
        }
    }
}